var express = require('express');
var router = express.Router();
const zlib = require('zlib');
const bodyParser = require('body-parser');
const request = require('request');
const cache = require('memory-cache');
const apiService = require('../service/apiService');
const apiBase = require('../service/apiBase');
const { createWorker } = require('tesseract.js');
const models = require('../db/models');
const iconv = require('iconv-lite');
const worker = createWorker();
var jsonParser = bodyParser.json();
const api = new apiService();
const base = new apiBase();
var urlencodedParser = bodyParser.urlencoded({ extended: false, limit:'10000kb' });
/* GET home page. */
router.get('/', function (req, res, next) {
	res.render('index', { title: 'Express' });
});
router.get('/test', function (req, res, next) {
	res.render('test', {title: 'Express'});
});
router.get('/enterprise', function (req, res, next) {
	res.render('enterprise', {title: 'Express'});
});
/* GET home page. */
router.post('/getHtml', urlencodedParser, function (req, res, next) {
	const url = req.body.url;
	const headers = {
		Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
		'Accept-Encoding': 'gzip, deflate, br',
		'Accept-Language': 'en-US,en;q=0.8',
		Connection: 'keep-alive',
		Host: 'www.liveinau.cn',
		Referer: url,
		'Sec-Fetch-Dest': 'document',
		'Sec-Fetch-Mode': 'navigate',
		'Sec-Fetch-Site': 'same-origin',
		'Sec-Fetch-User': '?1',
		'Upgrade-Insecure-Requests': '1',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
	};
	const j = request.jar();
	request({url,headers, jar: true }, function (error, response, body) {
		if (!error && response.statusCode === 200) {
			let content = body;
			var REG_BODY = /<body[^>]*>([\s\S]*)<\/body>/;
			var result = REG_BODY.exec(content);
			if (result && result.length === 2) {
				content = result[1];
			}
			var SCRIPT_REGEX = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
			while (SCRIPT_REGEX.test(content)) {
				content = content.replace(SCRIPT_REGEX, '');
			}
			res.send(content);
		} else {
			res.send('{error:404}');
		}
	});
});
router.post('/getDetail', urlencodedParser, function (req, res, next) {
	const url = req.body.url;
	const headers = {
		'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
		'accept-encoding': 'gzip, deflate, br',
		'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
		'cache-control':'max-age=0',
		'sec-ch-ua':'" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
		'sec-ch-ua-mobile':'?0',
		'sec-ch-ua-platform': '"Windows"',
		'sec-fetch-dest': 'document',
		'sec-fetch-mode': 'navigate',
		'sec-fetch-site': 'same-origin',
		'sec-fetch-user': '?1',
		'upgrade-insecure-requests': '1',
		'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
	};
	request({url:"https://www.tianyancha.com" + url,headers,encoding: null, jar: true}, function (error, response, body) {
		if (!error && response.statusCode === 200) {
			zlib.gunzip(body, function (error, data) {
				let content = data.toString();
				const REG_BODY = /<body[^>]*>([\s\S]*)<\/body>/;
				const result = REG_BODY.exec(content);
				if (result && result.length === 2) {
					content = result[1];
				}
				const SCRIPT_REGEX = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
				while (SCRIPT_REGEX.test(content)) {
					content = content.replace(SCRIPT_REGEX, '');
				}
				res.json({ status:"success",content,res: response});
			})
		} else {
			res.json({ error: response });
		}
	});
});
router.post('/isIncluded', urlencodedParser, function (req, res, next) {
	const url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=' + encodeURIComponent(req.body.url);
	const proxy = req.body.proxy;
	console.log(cache.get(proxy || 'local'));
	const header = cache.get(proxy || 'local') ? cache.get(proxy || 'local') : {};
	const headers = {
		Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
		'Accept-Encoding': 'gzip, deflate',
		'Accept-Language': 'en-US,en;q=0.8',
		'Cache-Control': 'max-age=0',
		Connection: 'keep-alive',
		Host: 'www.baidu.com',
		Referer: url,
		'Sec-Fetch-Dest': 'document',
		'Sec-Fetch-Mode': 'navigate',
		'Sec-Fetch-Site': 'same-origin',
		'Sec-Fetch-User': '?1',
		'Upgrade-Insecure-Requests': '1',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
		...header,
	};
	const j = request.jar();
	request({ url, headers, encoding: null, proxy, time: true, timeout: 100, jar: j }, function (error, response, body) {
		if (!error && response.statusCode === 200) {
			cache.put(proxy || 'local', { 'set-cookie': response.headers['set-cookie'][0] });
			if (response.headers['content-encoding'] == 'gzip') {
				zlib.gunzip(response.body, function (error, data) {
					if (!error) {
						response.body = data.toString();
						let status;
						if (response.body.indexOf('很抱歉，没有找到与') !== -1) {
							status = 1;
						} else if (response.body.indexOf('百度安全验证') !== -1) {
							status = 2;
						} else {
							status = 3;
						}
						res.json({ status });
					} else {
						response.body = body;
						res.json({ status: 2 });
					}
				});
			} else {
				let status;
				if (response.body.indexOf('很抱歉，没有找到与') !== -1) {
					status = 1;
				} else if (response.body.indexOf('百度安全验证') !== -1) {
					status = 2;
				} else {
					status = 3;
				}
				res.json({ status });
			}
		} else {
			res.json({ status: 4 });
		}
	});
});
router.post('/upload', urlencodedParser, function (req, res, next) {
	const list = req.body.list;
	request({ url: 'http://newadmin.liuxuego.com/api/data/article/batch', json: true, body: { data: list }, method: 'POST' }, function (error, response, body) {
		if (!error && response.statusCode === 200) {
			res.json({ status: 'success' });
		} else {
			res.json({ status: 'error' });
		}
	});
});
router.post('/getProxyIp', urlencodedParser, function (req, res, next) {
	const page = req.body.page;
	let proxyList = [];
	request('https://www.kuaidaili.com/free/inha/' + page, function (error, response, body) {
		if (!error && response.statusCode === 200) {
			let content = body;
			const REG_BODY = /<table class="table table-bordered table-striped[^>]*>([\s\S]*)<\/table>/;
			const result = REG_BODY.exec(content);
			if (result && result.length === 2) {
				content = result[1];
			}
			const listIp = content.match(/<td data-title="IP".*<\/td>/g);
			const listPort = content.match(/<td data-title="PORT".*<\/td>/g);
			listIp.forEach((e, index) => {
				const ip = e.match(/(\d+\.\d+\.\d+\.\d+)/)[0];
				const port = listPort[index].split('PORT">')[1].split('</td>')[0];
				if (ip && port) {
					proxyList.push('http://' + ip + ':' + port);
				}
			});
			res.json({ list: proxyList });
		} else {
			res.json({ status: 'error' });
		}
	});
});
router.post('/testProxyIp', urlencodedParser, function (req, res, next) {
	const proxyList = req.body.list;
	const promiseList = [];
	for (let i = 0; i < proxyList.length; i++) {
		promiseList.push(
			new Promise((resolve, rejects) => {
				request({ url: 'http://www.fglobal.cn/', timeout: 400, proxy: proxyList[i] }, function (error, response, body) {
					if (!error && response.statusCode === 200) {
						resolve(true);
					} else {
						resolve(false);
					}
				});
			})
		);
	}
	Promise.all(promiseList).then((response) => {
		for (let i = 0; i < response.length; i++) {
			if (!response[i]) {
				proxyList.splice(i, 1);
				response.splice(i, 1);
				i--;
			}
		}
		res.json({ list: proxyList });
	});
});
router.post('/getOrgProxy', urlencodedParser, function (req, res, next) {
	const url = req.body.url;
	request(url, function (error, response, body) {
		if (!error && response.statusCode === 200) {
			let list = body.split(/[\r\n|\n]/);
			res.json({ list: list });
		} else {
			res.json({ status: 'error' });
		}
	});
});

router.post('/getArticleGrade', urlencodedParser, async function (req, res, next) {
	const content = req.body.content;
	// let baiduToken = JSON.parse(cache.get('baiduToken'));
	// if (!baiduToken) {
	// 	const tokenResult = await api.request({
	// 		url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=OmQlE6eG9LbTLzoDvWulHqSL&client_secret=H3fQPFpXtik53jN88ZoOYscOxHrljGN6',
	// 		method: 'post',
	// 		rejectUnauthorized: false,
	// 	});
	// 	if (tokenResult.error) {
	// 		res.json({ status: 'get baidutoken error' });
	// 		return;
	// 	}
	// 	baiduToken = JSON.parse(tokenResult);
	// 	cache.put('baiduToken', tokenResult);
	// }
	let imageData, code;
	while(!/^[0-9]{4}$/.test(code)){
		imageData = await base.getVerifyCode();
		if (imageData.error) {
			break;
		}
		await worker.load();
		await worker.loadLanguage('eng');
		await worker.initialize('eng');
		await worker.setParameters({
			tessedit_char_whitelist: '0123456789',
			user_defined_dpi:'200'
		});
		const workerdata = await worker.recognize(imageData);
		code = workerdata.data.text.replace(/\n|\r\n|\s/g,'');
		console.log("code:",[code]);
	}
	
	
	// const image = Buffer.from(imageData, 'utf8').toString('base64');
	// const codeData = await api.request({
	// 	url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' + baiduToken.access_token,
	// 	method: 'post',
	// 	form: { language_type: 'ENG', image: image },
	// 	rejectUnauthorized: false,
	// });
	// if (codeData.error) {
	// 	res.json({ status: 'get verifyCode error' });
	// 	return;
	// }
	// if (codeData.words_result_num<1) {
	// 	res.json({ status: 'get verifyCode null' });
	// 	console.log('get verifyCode null');
	// 	return;
	// }
	// const code = JSON.parse(codeData).words_result[0].words;
	const articleResult = await api.request({
		url: 'http://www.zhipaiwu.com/index.php/Semblance_check/article_add.html',
		jar: true,
		method: 'post',
		encoding: 'utf-8',
		headers: {
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
			Accept: 'image/webp,image/apng,image/*,*/*;q=0.8',
			Host: 'www.zhipaiwu.com',
			Referer: 'http://www.zhipaiwu.com/index.php/Semblance_check/list1/.html',
		},
		formData: {
			content: content,
			code: code,
		},
	});
	if (articleResult.error) {
		res.json({ error: 'get articleGrade error' });
		return;
	}
	const urlResult = articleResult.match(/location.href='(.*)';/);
	if (!urlResult || urlResult.length < 2) {
		res.json({ error: 'get articleResultUrl error' });
	}
	const url = 'http://www.zhipaiwu.com' + urlResult[1];
	const gradeResult = await api.request({
		url: url,
		jar: true,
		method: 'get',
		encoding: null,
		headers: {
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
			Accept: 'image/webp,image/apng,image/*,*/*;q=0.8',
			Host: 'www.zhipaiwu.com',
			Referer: 'http://www.zhipaiwu.com/index.php/Semblance_check/article_add.html',
		},
	});
	const grade = gradeResult.match(/<span style="color:#f60;font-weight:bold;font-size:22px;" id="radio_num">(.*)<\/span>/)[1];
	const num = gradeResult.match(/<span id="article_num" style="color:#f60;font-weight:bold;font-size:22px;">(.*)<\/span>/)[1];
	res.json({ grade, num });
	// res.json({ data:null});
});
router.get('/getEnterprise',async function (req, res, next) {
	try{
		let list = [];
		list = await models.enterprise.findAll({attributes:['id','enterpriseName','email','website','phone1','phone2','phone3','phone4','phone5','phone6','phone7','phone8','phone9','phone10','webemail','webphone','webaddress','isget','remark','key','result'],where:{isget:"finished"},order:[['key','asc']],limit:1000});
		res.json({ list });
	}catch(err){
		console.log("error:",err);
	}
});
router.post('/setEnterpriseContact',urlencodedParser,async function (req, res, next) {
	try{
		const item = req.body;
		result = await models.enterprise.update({
			webemail:item.webemail,
			webaddress:item.webaddress,
			webphone:item.webphone,
			isget:item.isget,
			remark:item.remark,
		},{where:{key:item.key}});
		res.json({ result });
	}catch(err){
		console.log("error:",err);
	}
});
router.post('/setEnterpriseResult',urlencodedParser,async function (req, res, next) {
	try{
		const item = req.body;
		result = await models.enterprise.update({
			result:item.result,
		},{where:{key:item.key}});
		res.json({ result });
	}catch(err){
		console.log("error:",err);
	}
});
router.post('/getWebHtml', urlencodedParser, function (req, res, next) {
	const url = req.body.url;
	const headers = {
		Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
		'Accept-Encoding': 'gzip, deflate, br',
		'Accept-Language': 'en-US,en;q=0.8',
		Connection: 'keep-alive',
		'Sec-Fetch-Dest': 'document',
		'Sec-Fetch-Mode': 'navigate',
		'Sec-Fetch-Site': 'same-origin',
		'Sec-Fetch-User': '?1',
		'Upgrade-Insecure-Requests': '1',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
	};
	request({url,headers, encoding: null ,timeout:10000}, function (error, response, body) {
		if (!error && response.statusCode === 200) {
			let content;
			if (response.headers['content-encoding'] == 'gzip') {
				zlib.gunzip(response.body, function (error, data) {
					if (!error) {
						response.body = data.toString();
						content = response.body;
						var REG_BODY = /<body[^>]*>([\s\S]*)<\/body>/;
						var result = REG_BODY.exec(content);
						if (result && result.length === 2) {
							content = result[1];
						}
						var SCRIPT_REGEX = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
						while (SCRIPT_REGEX.test(content)) {
							content = content.replace(SCRIPT_REGEX, '');
						}
						res.send({data:content});
					} else {
						response.body = body;
						res.json({ status: 2 });
					}
				});
			} else {
				if(response.headers['content-type'].indexOf("utf-8") === -1){
					content = iconv.decode(response.body, 'gbk').toString();
				}else{
					content = response.body.toString();
				}
				var REG_BODY = /<body[^>]*>([\s\S]*)<\/body>/;
				var result = REG_BODY.exec(content);
				if (result && result.length === 2) {
					content = result[1];
				}
				var SCRIPT_REGEX = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
				while (SCRIPT_REGEX.test(content)) {
					content = content.replace(SCRIPT_REGEX, '');
				}
				res.send({data:content});
			}
		} else {
			res.send({error:"error"});
		}
	});
});
module.exports = router;
